package beans;

import java.io.StreamCorruptedException;
import java.util.LinkedList;

import javax.media.jai.JAI;

import ourImage.OurPlanarImage;
import Listeners.ImageListener;
import events.ImageEvent;

/**
 * 
 * @author Benjamin
 *
 */
public class SinkBean extends BaseBean implements ImageListener, Runnable {

	private BaseBean in;
	private LinkedList<ImageListener> listeners = new LinkedList<>();

	@Override
	public void run() {
		System.out.println("Pipe gestartet in Pull-Mode!");
		OurPlanarImage img = null;
		try {

			while ((img = read()) != null) {
				img = in.read();
			}
		} catch (StreamCorruptedException ex) {
			System.out.println(ex.getMessage());
		}
		JAI.create("filestore", img, "result.jpg", "JPEG");
	}

	@Override
	public OurPlanarImage read() throws StreamCorruptedException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void write(OurPlanarImage img) throws StreamCorruptedException {
		JAI.create("filestore", img, "result.jpg", "JPEG");

	}

	@Override
	public String getFilterName() {
		return "Sink";
	}

	@Override
	public boolean isFilterEnabled() {
		return true;
	}

	/*
	 * BeanBox Events
	 */
	@Override
	public void onImageProceed(ImageEvent e) {
		BaseBean src = (BaseBean) e.getSource();
		try {
			OurPlanarImage img = src.read();
			in.write(img);
		} catch (StreamCorruptedException ex) {
			System.out.println(ex.getMessage());
		}
	}

}
